//
//  ZJTabbar.m
//  Weibo29
//
//  Created by 曾静 on 16/6/28.
//  Copyright © 2016年 qq. All rights reserved.
//

#import "ZJTabbar.h"
#import "UIView+Fram.h"

@interface ZJTabbar()

//定义中间的按钮
@property(nonatomic,strong)UIButton *composeBtn;


@end


@implementation ZJTabbar


//懒加载按钮
-(UIButton *)composeBtn{
    
    if (!_composeBtn) {
       
        //使用定义好的属性记得使用strong,不然创建出来就释放掉了
        _composeBtn = [[UIButton alloc]init];
        
        [_composeBtn  setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        
        
        [_composeBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        [_composeBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        
        [_composeBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        [_composeBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        [_composeBtn sizeToFit];
    }
    
    return _composeBtn;
}



-(void)layoutSubviews{
    
    //一定记得这句话
    [super  layoutSubviews];

   
    
    CGFloat width = self.width / 5.0;
    
    CGFloat height = self.height;
    
    NSInteger index = 0;
    
    for (UIView * BarButton  in self.subviews) {
        
        if ([BarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]){
            
            
            BarButton.x = width * index;
            BarButton.width = width;
            
            
            if (index == 1) {
                index++;
                
                //设置frmae,直接设置center是不起作用的,因为center是相对与父控件的center
                _composeBtn.frame = CGRectMake(width * index, 0, width, height);
            }
            
            index++;

        }
        
    }

    
}
//点击按钮记得push控制器modal的形式
- (void)addBtnClick{
    
    if (self.callBack) {
        self.callBack();
    }
    
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    
    if ( self = [super initWithFrame:frame]) {
        
        //不能使用_composeBtn,只能使用self.composeBtn,_composeBtn是没有进行懒加载的属性
        
        [self addSubview:self.composeBtn];
    }
    
    return self;
}

@end
